<template>
  <div class="article">
    <headerPhoto :height="headerHeight" class="article-header">
      <router-link to="/search">
        <div class="search">
          <input type="text" placeholder="请输入想要搜索的文章">
          <!-- <i class="bi bi-search text-light"></i> -->
        </div>
      </router-link>
    </headerPhoto>
    <waveView style="transform: translateY(-45px);"></waveView>
    <div class="container">
      <div class="title">
        <div class="box" v-for="(item, index) in cate" :key="index">
          <router-link :class="{ active: route.query.cate == item }"
            :to="{ path: 'articleClass', query: { cate: item } }" exact>
            <div class="left">
              <i class="bi bi-person-workspace"></i>
            </div>
            <div class="right">
              <span>{{ item }}</span>
            </div>
          </router-link>
        </div>
        <!-- <div class="box">
          <router-link active-class="active" :to="{ path: 'studyMore', query: { cate: '技能学习' } }">
            <div class="left">
              <i class="bi bi-x-diamond-fill"></i>
            </div>
            <div class="right">
              <span>技能学习</span>
            </div>
          </router-link>
        </div>
        <div class="box">
          <router-link active-class="active" :to="{ path: 'hobbyPart', query: { cate: '兴趣爱好' } }">
            <div class="left">
              <i class="bi bi-calendar2-heart"></i>
            </div>
            <div class="right">
              <span>兴趣爱好</span>
            </div>
          </router-link>
        </div> -->


      </div>
      <div class="recom">
        <i class="bi bi-book"></i>
        <span class="ms-2">文章列表</span>
      </div>
      <router-view></router-view>
    </div>
  </div>
</template>

<script setup lang="ts">
import headerPhoto from '@/component/headerPhoto.vue';
import waveView from '@/component/waveView.vue';
import { ref } from 'vue';
import { getAllArticle } from '@/api/index';
import { useRoute, useRouter } from 'vue-router';
const headerHeight = ref<number>(280);
const route = useRoute()
const router = useRouter()
interface IArticle {
  [key: string | number]: string | number
}
// 一级分类数组
const cate = ref<(string | number | undefined)[]>([]);
// 获取文章列表数据
const getAllArticleFn = async () => {

  await getAllArticle().then(
    res => {
      if (res.data.code == 200) {
        // console.log('文章列表数据', res.data.result)
        cate.value = [...new Set((res.data.result as IArticle[]).map(item => item.cate))]
        // 如果当前路由没有cate参数，且分类数据存在，则默认跳转到第一个分类
        if (!route.query.cate && cate.value.length > 0 && cate.value[0]) {
          router.push({
            path: 'articleClass',
            query: { cate: cate.value[0] }
          });
        }
      }
      // console.log("列表", cate.value)
    },
    errr => {
      console.log(errr)
    }
  )
}
getAllArticleFn()

</script>

<style lang="less" scoped>
.article {
  padding-bottom: 50px;


  .search {
    width: 300px;
    height: 40px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border: 2px solid #d6fe67;
    border-radius: 20px;

    input {
      width: calc(100% - 40px);
      height: 100%;
      border-radius: 15px;
      border: none;
      outline: none;
      padding-left: 15px;
      background-color: transparent;
      color: #fff;

      &::-webkit-input-placeholder {
        color: rgb(255, 255, 255);
      }
    }

    i {
      display: block;
      width: 30px;
      height: 30px;
      font-size: 18px;
      background-size: cover;
    }
  }


  .container {
    .recom {
      display: none;
      transform: translateY(50px);

      i {
        font-size: 25px;
      }

      span {
        display: inline-block;
        padding-bottom: 10px;
      }
    }


    @media (max-width: 768px) {
      .recom {
        display: block;
      }

      .title {
        height: 230px;

        .box {
          // display: none;
          width: 100% !important;
          margin-right: 0 !important;
          margin-bottom: 20px;
        }
      }
    }




    .title {
      display: flex;
      flex-wrap: wrap;

      .box {
        width: calc(100% / 4);
        padding: 20px 15px;
        background-color: rgba(255, 255, 255, .5);
        border-radius: 10px;
        transform: translateY(-35px);
        box-shadow: 0 0 10px rgba(0, 0, 0, .2);
        margin-right: 15px;
        transition: all .2s;
        margin-bottom: 10px;

        &:hover {
          transform: translateY(-40px);
        }

        a {
          display: flex;
          align-items: center;

          &.active {
            color: #FFA500;
          }

          .left {
            font-size: 24px;
            background-size: cover;
          }

          .right {
            margin-left: 15px;

            span {
              vertical-align: middle;


            }
          }
        }
      }
    }
  }
}
</style>
